<?php

namespace App\Http\Controllers\Admin;

use App\Model\AdminUser;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class AdministrationController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = AdminUser::orderBy('id','desc')->get();
        $total = AdminUser::count();
        return view('admin.administration.admin-list',compact('data','total'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.administration.admin-add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $m = new AdminUser();
        $m->name = $request->input('adminName');
        $m->email = $request->input('email');
        if ($request->input('password') == $request->input('password2')){
            $m->password = bcrypt($request->input('password'));
        }else {
            return $this->error('两次输入密码不相同');
        }
        $m->sex = $request->input('sex');
        $m->tel = $request->input('phone');
        $m->role = $request->input('adminRole');
        $m->remarks = $request->input('remarks');
        if ($m->save()){
            return $this->success('添加成功');
        }else {
            return $this->error('添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $item = AdminUser::where('id',$id)->first();
        return view('admin.administration.admin-edit',compact('item'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $m = AdminUser::find($id);
        $m->name = $request->input('adminName');
        $m->email = $request->input('email');
        $m->sex = $request->input('sex');
        $m->tel = $request->input('phone');
        $m->role = $request->input('adminRole');
        $m->remarks = $request->input('remarks');
        if ($m->save()){
            return $this->success('修改成功');
        }else {
            return $this->error('修改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = AdminUser::where('id',$id)->delete();
        if ($data){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }
}
